package hz.mobileproject.pda.presenter;



import android.content.Context;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.listener.ICommonV;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.utils.Base64;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class PEdu extends XPresent<ICommonV> {
    private  String token;
    private Gson gson;
    private Request_data data;
    private String body_data;
    private RequestBody body;


    public PEdu(Context context) {
        gson = new Gson();
        data = new Request_data();

    }

    //上传教育谈话

    /**
     *
     * @param request_data  json, bean的json,上传文件附带的信息
     */
    public void uploadData(String request_data ,String filePath){
        data.setData(Base64.getBase64(request_data));
        body_data=  gson.toJson(data);
        //上传教育谈话.  上传文件是放在?  后面拼接的
//        uploadEdu(body_data,filePath);
    }

    public void uploadData(int type){
        getType(type);
    }



    public void loadData(Object request_data ,int type,Context context){
        String date= gson.toJson(request_data);
        Log.e("load",date);
        data.setData(Base64.getBase64(date));
        body_data=  gson.toJson(data);
        body= RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),body_data);
        token = context.getSharedPreferences("app_login", Context.MODE_PRIVATE).getString("token","");
        getDataBody(body,type);

    }


    private void getDataBody(RequestBody body,int type) {
        switch (type){
            case 1://2.1.12	查询本部门所有教育谈话
                Api.getGankService().queryEducationalLog(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().showError(new IllegalStateException(""));
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                            }
                        });
                break;
            case 2://2.1.12 获取民警信息
                Api.getGankService().getloadPoliceInfo(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().showError(new IllegalStateException(""));
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().showData(json,2);
                            }
                        });
                break;
            case 3://2.1.12	获取罪犯信息
                Api.getGankService().getloadPrisonerInfo(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().showError(new IllegalStateException(""));
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().showData(json,3);
                            }
                        });
                break;
            case 4://

                Api.getGankService().getType(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                getV().showError(new IllegalStateException(""));
                            }
                            @Override
                            public void onNext(Response_data gankResults) {
                                LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                                String json= Base64.getFromBase64(gankResults.getData());
                                getV().showData(json,4);
                            }
                        });

                break;

        }



    }

    private void getType(int type) {
        Api.getGankService().getType(body,token)
                .compose(XApi.<Response_data>getApiTransformer())
                .compose(XApi.<Response_data>getScheduler())
                .subscribe(new ApiSubscriber<Response_data>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().showError(new IllegalStateException(""));
                    }
                    @Override
                    public void onNext(Response_data gankResults) {
                        LogUtils.e(Base64.getFromBase64(gankResults.getData()));
                        String json= Base64.getFromBase64(gankResults.getData());
                        getV().showData(json,4);
                    }
                });
    }


//    private void uploadEdu(String uploaddata,String file_path ) {
//        BaiduParams params = new BaiduParams();
//
//        // 有上传文件时使用multipart表单, 否则上传原始文件流.
////         params.setMultipart(true);
//        // 上传文件方式 1
////        params.file = new File(uploaddata);
//        params.addBodyParameter("file",new File(file_path),"application/octet-stream");
//        // 上传文件方式 2
////        params.addBodyParameter("uploadFile", new File(filePath));
//        params.addBodyParameter("data", uploaddata,"application/octet-stream");
//
//        x.http().post(params,
//                new Callback.CommonCallback<List<BaiduResponse>>() {
//                    @Override
//                    public void onSuccess(List<BaiduResponse> result) {
//                        try {
//                            String json = result.get(0).getTest();
//                            JSONObject jsonObject = new JSONObject(json);
//                            String rtnCode = jsonObject.getString("rtnCode");
//                            if(rtnCode.equals("0")){
//                                getV().showData("{data:'data'}",200);
//                                return;
//                            }
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                        getV().showData("{data:'data'}",502);
//                    }
//
//                    @Override
//                    public void onError(Throwable ex, boolean isOnCallback) {
//                        getV().showData("{data:'data'}",502);
//                    }
//
//                    @Override
//                    public void onCancelled(CancelledException cex) {
//                        Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
//                    }
//
//                    @Override
//                    public void onFinished() {
//
//                    }
//                });
//
//    }

}

